import GameObject from "./GameObject";
import GameConst from "./GameConst";
import Utils from "./utils";
import SoundsManager from "./soundmanager";

export default class BonusIndicator extends GameObject {
    constructor(t, n, target, bonusType) {
        super();
        var s = this;

        this.borders = [];
        this.isMovingToTarget = false;
        this.isHiding = false;
        this.horizontal = false;
        this.target = target;
        this.bonusType = bonusType;
        var a1x = GameConst.level.gridToStageX(t.x);
        var a1y = GameConst.level.gridToStageY(t.y);
        var a2x = GameConst.level.gridToStageX(n.x);
        var a2y = GameConst.level.gridToStageY(n.y);
        var container = new Laya.Sprite;
        var c = 250;
        this.horizontal = t.y == n.y;
        for (var ii = 0; ii < 2; ++ii) {
            var image = Utils.createBitmap("bonus_line_side");
            image.pivotX = image.width;
            image.pivotY = image.height / 2;
            image.scaleX = image.scaleY = GameConst.TILE_SIZE_FACTOR;
            //var v = ii == 0 ? t : n;
            image.x = ii == 0 ? a1x : a2x;
            image.y = ii == 0 ? a1y : a2y;
            image.rotation = t.y == n.y ? ii == 0 && t.x < n.x || ii == 1 && n.x < t.x ? 0 : 180 : ii == 0 && t.y < n.y || ii == 1 && n.y < t.y ? 90 : -90;
            container.addChild(image);
            this.borders.push(image);
            Laya.Tween.to(image,{
                x: (a1x + a2x) / 2,
                y: (a1y + a2y) / 2
            }, c, Laya.Ease.cubicIn);
        }
        image = Utils.createBitmap("bonus_line_center");
        var m = (Utils.distanceBetweenPoints(a1x, a1y, a2x, a2y) + .3) / (image.width - 2.9);
        image.scaleX = m;
        image.scaleY = GameConst.TILE_SIZE_FACTOR;
        container.addChildAt(image, 0);
        image.x = (a1x + a2x) / 2;
        image.y = (a1y + a2y) / 2;
        image.pivotX = image.width / 2;
        image.pivotY = image.height / 2;
        Laya.Tween.to(image, {
            scaleX: 0
        }, c, Laya.Ease.cubicIn, Laya.Handler.create(this,this.moveToTarget));
        image.rotation = t.y == n.y ? 0 : 90;
        this.borders.push(image);
        image = Utils.createBitmap("bonus_new");
        container.addChild(image);
        image.x = (a1x + a2x) / 2;
        image.y = (a1y + a2y) / 2;
        image.scaleX = .3;
        image.scaleY = .3;
        image.alpha = .3;
        image.pivotX = image.width / 2;
        image.pivotY = image.height / 2;
        this.borders.push(image);
        Laya.Tween.to(image,{
            alpha: 1,
            scaleX: 1,
            scaleY: 1
        }, c, Laya.Ease.cubicIn);
        this.sprite = container;
        Utils.addChild(this.sprite, GameConst.level.bonusIndicatorLayer);
        target.setTempBlock(true, .1);
        SoundsManager.instance.playSound("bonus_show");
    }

    destroy() {
        if (this.isDestroyed)
            return;
        Laya.Tween.clearAll(this.borders[3]);
        Laya.Tween.clearAll(this.sprite);
        this.isMovingToTarget = true;
        for (var t = 0; t < this.borders.length; ++t)
            Utils.removeClip(this.borders[t]);
        this.borders = null;
        super.destroy();
    }
}
var t = BonusIndicator;

t.prototype.moveToTarget = function () {
    this.borders[0].visible = this.borders[1].visible = false;
    this.isMovingToTarget = true;
    var x = GameConst.level.gridToStageX(this.target.x);
    var y = GameConst.level.gridToStageY(this.target.y);
    var r = this.sprite.getBounds();
    x -= r.x + r.width / 2;
    y -= r.y + r.height / 2;
    Laya.Tween.to(this.sprite,{
        x: x,
        y: y
    }, 500, Laya.Ease.cubicIn,Laya.Handler.create(this,this.hide));
    this.isHiding = true;
};
t.prototype.hide = function () {
    var e = this;
    if (this.isDestroyed)
        return;
    if (this.target && this.target.object) {
        this.target.object.setBonusType(this.bonusType);
        this.target.setTempBlock(false, -1);
        SoundsManager.instance.playSound("bonus_set");
    }
    Laya.Tween.to(this.borders[3],{
        alpha: 0,
        scaleX: 0,
        scaleY: 0
    }, 800, Laya.Ease.cubicOut,Laya.Handler.create(this,this.destroy));
};
t.prototype.updateTarget = function (e) {
    this.target = e;
    Laya.Tween.clearAll(this.sprite);
    Laya.Tween.clearAll(this.borders[3]);
    this.moveToTarget();
};
t.prototype.checkTargetObject = function () {
    if (this.isMovingToTarget && (!this.target.object || this.target.object.isMoving || this.target.isWaitingForClear)) {
        this.target = null;
        GameConst.level.spawnBonus(this.bonusType, null, null, this);
    }
};
t.prototype.update = function (e) {
    if (!this.isMovingToTarget && false) {
        var t = this.borders[2];
        var n = t.scaleX * 25 - 2;
        for (var r = 0; r < 2; ++r) {
            var i = this.borders[r];
            i.x = t.x + (this.horizontal ? (r == 1 ? 1 : -1) * n / 2 : 0);
            i.y = t.y + (!this.horizontal ? (r == 1 ? 1 : -1) * n / 2 : 0);
        }
    }
    this.checkTargetObject();
    if (!this.isDestroyed)
        this.target.setTempBlock(true, .1);
};
